#include <func.h>

int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,3);
    int fdr=open(argv[1],O_RDONLY);
    int fdw=open(argv[2],O_WRONLY);
    fd_set rdset;
    struct timeval timeout;
    char buf[4096]={0};
    while(1)
    {
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(fdr,&rdset);
        timeout.tv_sec=5;
        int ret=select(fdw+1,&rdset,NULL,NULL,&timeout);
        if(ret==0)
        {
            printf("time out\n");
            break;
        }
        else
        {
            
            if(FD_ISSET(STDIN_FILENO,&rdset))
            {
            
                memset(buf,0,sizeof(buf));
                ssize_t sret=read(STDIN_FILENO,buf,sizeof(buf));
                if(sret==0)
                {

                    printf("close\n");
                    break;
                }  
               write(fdw,buf,strlen(buf));
            }
            if(FD_ISSET(fdr,&rdset))
            {
                  memset(buf,0,sizeof(buf));
                  ssize_t sret=read(fdr,buf,sizeof(buf));
                  if(sret==0)
                  {
                     printf("game over\n");
                     break;
                  }
                  printf("sret=%ld buf=%s\n",sret,buf);
            }
           
        }
    }
    close(fdw),close(fdr);
    return 0;
}

